package com.zmn.plat.admin.controller.product;

import com.google.gson.Gson;
import com.zmn.common.dto.ResultDTO;
import com.zmn.common.utils.string.StringUtil;
import com.zmn.plat.admin.config.Ueditor;
import com.zmn.plat.business.interfaces.pic.PicUploadBService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.List;

/**
 * 类描述:ueditor上传图片
 *
 * @author linfeng
 * @since 2019/09/06/10:46
 */
@Controller
@RequestMapping("/ueditor/")
public class UeditorUploadImageController {
    private Logger logger = LoggerFactory.getLogger(UeditorUploadImageController.class);

    @Resource
    PicUploadBService picUploadBService;

    @RequestMapping(value = "/uploadImageData", produces = "appliction/json;charset=utf-8")
    @ResponseBody
    public String uploadImageData(HttpServletRequest request,
                                  @RequestParam(value = "folderName", required = false) String folderName) {
        Gson gson = new Gson();
        Ueditor msg = uploadFile(request, folderName);
        return gson.toJson(msg);
    }

    private Ueditor uploadFile(HttpServletRequest request, String folderName) {
        Ueditor image = new Ueditor();
        List<MultipartFile> files = ((MultipartHttpServletRequest) request).getFiles("upfile");
        try {
            String fileName = files.get(0).getOriginalFilename();
            if (files.get(0).getSize() > 5120000) {
                image.setState("Picture is too large,please choose new");
                image.setUrl("none");
                image.setOriginal(fileName);
                image.setTitle(fileName);
                return image;
            }
            logger.info("接收到上传数据，图片名称为 ：" + fileName);
            byte[] byt = files.get(0).getBytes();
            InputStream inputStream = new ByteArrayInputStream(byt);
            String directory = StringUtil.isNotBlank(folderName) ? folderName : "plat/product/desc/";
            directory += System.currentTimeMillis() + "/";
            logger.info("调用方法UeditorUploadImageController的uploadPicUnpressed方法");
            ResultDTO result = picUploadBService.uploadPicUnpressed(directory, fileName, inputStream);

            image.setUrl("");
            if (result.getStatusBool()) {
                image.setUrl(picUploadBService.getPicUrl(directory, fileName.toString()));
            }
            logger.info("图片上传成功,上传路径为 ：");

            image.setState("SUCCESS");
            image.setOriginal(fileName);
            image.setTitle(fileName);
        } catch (IOException e) {

            logger.error("图片上传失败，失败原因:" + e.getMessage());
            image.setState("FAIL");
        }
        return image;
    }
}
